<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="../../lib/network-simulator.html">

<script>
(function() {

var assert = chai.assert;

describe('results script', function() {

  var MockResultsBaseURL = 'https://storage.googleapis.com/chromium-layout-test-archives/Mock_Builder/results/layout-test-results';

  it('should trim extensions', function() {
    assert.equal(results._trimExtension('xyz'), 'xyz');
    assert.equal(results._trimExtension('xy.z'), 'xy');
    assert.equal(results._trimExtension('x.yz'), 'x');
    assert.equal(results._trimExtension('x.y.z'), 'x.y');
    assert.equal(results._trimExtension('.xyz'), '');
    assert.equal(results._trimExtension(''), '');
  });

  it('should create failure info', function() {
    var failureInfo = results.failureInfo('userscripts/another-test.html', 'Mock Builder', 'FAIL PASS');
    assert.deepEqual(failureInfo, {
        'testName': 'userscripts/another-test.html',
        'builderName': 'Mock Builder',
        'failureTypeList': ['FAIL', 'PASS'],
    });
  });

  it('should get result kind', function() {
    assert.equal(results.resultKind('https://example.com/foo-actual.txt'), 'actual');
    assert.equal(results.resultKind('https://example.com/foo-expected.txt'), 'expected');
    assert.equal(results.resultKind('https://example.com/foo-diff.txt'), 'diff');
    assert.equal(results.resultKind('https://example.com/foo.bar-actual.txt'), 'actual');
    assert.equal(results.resultKind('https://example.com/foo.bar-expected.txt'), 'expected');
    assert.equal(results.resultKind('https://example.com/foo.bar-diff.txt'), 'diff');
    assert.equal(results.resultKind('https://example.com/foo-actual.png'), 'actual');
    assert.equal(results.resultKind('https://example.com/foo-expected.png'), 'expected');
    assert.equal(results.resultKind('https://example.com/foo-diff.png'), 'diff');
    assert.equal(results.resultKind('https://example.com/foo-pretty-diff.html'), 'diff');
    assert.equal(results.resultKind('https://example.com/foo-wdiff.html'), 'diff');
    assert.equal(results.resultKind('https://example.com/foo-xyz.html'), 'unknown');
  });

  it('should get result type', function() {
    assert.equal(results.resultType('https://example.com/foo-actual.txt'), 'text');
    assert.equal(results.resultType('https://example.com/foo-expected.txt'), 'text');
    assert.equal(results.resultType('https://example.com/foo-diff.txt'), 'text');
    assert.equal(results.resultType('https://example.com/foo.bar-actual.txt'), 'text');
    assert.equal(results.resultType('https://example.com/foo.bar-expected.txt'), 'text');
    assert.equal(results.resultType('https://example.com/foo.bar-diff.txt'), 'text');
    assert.equal(results.resultType('https://example.com/foo-actual.png'), 'image');
    assert.equal(results.resultType('https://example.com/foo-expected.png'), 'image');
    assert.equal(results.resultType('https://example.com/foo-diff.png'), 'image');
    assert.equal(results.resultType('https://example.com/foo-pretty-diff.html'), 'text');
    assert.equal(results.resultType('https://example.com/foo-wdiff.html'), 'text');
    assert.equal(results.resultType('https://example.com/foo.xyz'), 'text');
  });

  it('fetchResultsURLS', function(done) {
    var simulator = new NetworkSimulator(assert, done);

    var probedURLs = [];
    simulator.probe = function(url) {
      probedURLs.push(url);
      if (url.endsWith('.txt'))
        return Promise.resolve();
      else if (/taco.+png$/.test(url))
        return Promise.resolve();
      else
        return Promise.reject();
    };
    simulator.runTest(function() {
      return Promise.resolve().then(function() {
        return results.fetchResultsURLs({
          'builderName': 'Mock Builder',
          'testName': 'userscripts/another-test.html',
          'failureTypeList': ['IMAGE', 'CRASH'],
        }).then(function(resultURLs) {
          assert.deepEqual(resultURLs, [
            MockResultsBaseURL + '/userscripts/another-test-crash-log.txt'
          ]);
        });
      }).then(function() {
        return results.fetchResultsURLs({
          'builderName': 'Mock Builder',
          'testName': 'userscripts/another-test.html',
          'failureTypeList': ['TIMEOUT'],
        }).then(function(resultURLs) {
          assert.deepEqual(resultURLs, []);
        });
      }).then(function() {
        return results.fetchResultsURLs({
          'builderName': 'Mock Builder',
          'testName': 'userscripts/taco.html',
          'failureTypeList': ['IMAGE', 'IMAGE+TEXT'],
        }).then(function(resultURLs) {
          assert.deepEqual(resultURLs, [
            MockResultsBaseURL + '/userscripts/taco-expected.png',
            MockResultsBaseURL + '/userscripts/taco-actual.png',
            MockResultsBaseURL + '/userscripts/taco-diff.png',
            MockResultsBaseURL + '/userscripts/taco-expected.txt',
            MockResultsBaseURL + '/userscripts/taco-actual.txt',
            MockResultsBaseURL + '/userscripts/taco-diff.txt',
          ]);
        });
      }).then(function() {
        return results.fetchResultsURLs({
          'builderName': 'Mock Builder',
          'testName': 'userscripts/another-test.html',
          'failureTypeList': ['LEAK'],
        }).then(function(resultURLs) {
          assert.deepEqual(resultURLs, [
            MockResultsBaseURL + '/userscripts/another-test-leak-log.txt'
          ]);
        });
      }).then(function() {
        assert.deepEqual(probedURLs, [
          MockResultsBaseURL + '/userscripts/another-test-expected.png',
          MockResultsBaseURL + '/userscripts/another-test-actual.png',
          MockResultsBaseURL + '/userscripts/another-test-diff.png',
          MockResultsBaseURL + '/userscripts/another-test-crash-log.txt',
          MockResultsBaseURL + '/userscripts/taco-expected.png',
          MockResultsBaseURL + '/userscripts/taco-actual.png',
          MockResultsBaseURL + '/userscripts/taco-diff.png',
          MockResultsBaseURL + '/userscripts/taco-actual.txt',
          MockResultsBaseURL + '/userscripts/taco-expected.txt',
          MockResultsBaseURL + '/userscripts/taco-diff.txt',
          MockResultsBaseURL + '/userscripts/another-test-leak-log.txt',
        ]);
      });
    });
  });
});

})();
</script>
